<template>
  <view style="margin: 0 11px;" >
    <view class="title" >指定人名单表---以更新{{this.userSaigeData.length}}组</view>
	<!-- 搜索框区域 -->
	<view class="search-container">
		<view class="search-input">
			<input type="text" placeholder="输入指定人姓名搜索" v-model="searchText" @input="filterData" />
			<image style="width: 20px; height: 20px;" src="/static/images/search.png" mode="aspectFit"
				class="search-icon"></image>
		</view>
	</view>
    <scroll-view class="table-container" scroll-x="true" scroll-y="true" :style="{ height: scrollHeight + 'px' }">
      <table class="table">
        <thead>
          <tr>
            <th>序号</th>
            <th>指定人</th>
            <th>鸽主</th>
            <th>地址</th>
            <th>足环号</th>
            <th>飞速</th>
            <th>名次</th>
            <th>总分</th>
            <th>备注</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item, index) in filteredData" :key="index" :style="[ item.remarks.remarks_a === '未确认' ? { color: 'red' } : {}]">
            <td>{{ index+1 }}</td>
            <td>{{ item.designated_person }}</td>
            <td>
              <view>{{ item.owner.owner_a }}</view>
              <view>{{ item.owner.owner_b }}</view>
            </td>
            <td>
              <view>{{ item.address.address_a }}</view>
              <view>{{ item.address.address_b }}</view>
            </td>
            <td>
              <div>{{ item.foot_ring_number.foot_ring_number_a }}</div>
              <div>{{ item.foot_ring_number.foot_ring_number_b }}</div>
            </td>
            <td>
              <div>{{ item.speed.speed_a }}</div>
              <div>{{ item.speed.speed_b }}</div>
            </td>
            <td>
              <div>{{ item.rank.rank_a }}</div> 
              <div>{{ item.rank.rank_b }}</div>
            </td>
            <td>
              <div>{{item.rank.rank_a+item.rank.rank_b }}</div>
            </td>
            <td>
              <div>{{ item.remarks.remarks_a }}</div>
              <div>{{ item.remarks.remarks_b  }}</div>
            </td>
          </tr>
          <td colspan="8" class="empty-tip">暂无赛事数据，请先添加参赛记录</td>
        </tbody>
      </table>
    </scroll-view>
  </view>
</template>

<script>
export default {
  name: 'CardGrid',
  props: {
    tableData: []
  },
  data() {

    return {
      userSaigeData: [], // 存储获取的数据
      loading: false, // 加载状态
      error: null, // 错误信息
      rowColors: [
        '#FF5733', // 第1行橙色
        
      ],
	  filteredData:[],
	  searchText:'',
      scrollHeight: 500,

    };
  },
  created() {
    this.getUserSaigeData();
  },
  methods: {
    // 获取user_saige_data数据
    async getUserSaigeData() {
      this.loading = true;
      this.error = null;
      try {
        // 检查本地缓存
        const cacheKey = 'user_saige_data_cache';
        const cacheData = uni.getStorageSync(cacheKey);
        const cacheTime = uni.getStorageSync(`${cacheKey}_time`);
        const now = Date.now();
        
        // 如果缓存存在且在1小时内
        if (cacheData && cacheTime && (now - cacheTime < 3600000)) {
          this.userSaigeData = cacheData;
		  this.filteredData = this.userSaigeData;
          this.loading = false;
          console.log('使用缓存数据');
          return;
        }
        
        // 调用云对象
        const crudOperations = uniCloud.importObject('crudOperations');
        const res = await crudOperations.list('Two_Feather_Data', {
          page: 1,
          pageSize: 20,
          orderBy: 'createTime',
          orderType: 'desc',
        });

        if (res.code === 200) {
         this.userSaigeData = res.data.list.map((item, index) =>({
            serial: index + 1 || "待分配",
            designated_person: item.designated_person?.trim() || "未指定",
            foot_ring_number: `${item.foot_ring_number_a || '无'} || ${item.foot_ring_number_b || '无'}`,
            owner: {
            	owner_a: item.owner_a || '无名',
            	owner_b: item.owner_b || '无名',
            },
            address: {
            	address_a: item.address_a?.replace(/\s+/g, '') || '地址未登记',
            	address_b: item.address_b?.replace(/\s+/g, '') || '地址未登记',
            },
            number: item.serial_number?.toString().padStart(4, '0') || '0000',
            speed: {
            	speed_a: item.speed_a ?? '-',
            	speed_b: item.speed_b ?? '-',
            },
            rank: {
            	rank_a: item.rank_a ?? '-',
            	rank_b: item.rank_b ?? '-',
            },
            foot_ring_number: {
            	foot_ring_number_a: item.foot_ring_number_a || '无',
            	foot_ring_number_b: item.foot_ring_number_b || '无',
            },
            color: {
            	color_a: item.color_a?.toLowerCase() || '无',
            	color_b: item.color_b?.toLowerCase() || '无',
            },
            serial_number: (parseInt(item.rank_a) + parseInt(item.rank_b)).toString() || '000',
            remarks: {
            	remarks_a: item.remarks_a === true ? '已确认' : (item.remarks_a === false ? '未确认' : (item.remarks_a || '暂无备注信息')),
            	remarks_b: item.remarks_b === true ? '已确认' : (item.remarks_b === false ? '未确认' : (item.remarks_b || '暂无备注信息')),
            },
          }))
          // 保存到缓存
          console.log('获取远程数据成功');
          uni.setStorageSync('user_saige_data_cache', this.userSaigeData);
          uni.setStorageSync('user_saige_data_cache_time', Date.now());
		this.filteredData = [...this.userSaigeData]; 
        } else {
          this.error = res.message;
        }
      } catch (e) {
        this.error = e.message || '获取数据失败';
        console.error('获取数据失败:', e);
      } finally {
        this.loading = false;
      }
    },

    calculateHeight() {
      // 计算可视区域高度，动态设置滚动区域高度
      const systemInfo = uni.getSystemInfoSync();
      this.scrollHeight = systemInfo.windowHeight - 100; // 减去标题和其他元素高度
    },
    applyCellStyle(index) {
      return {
        width: this.columnWidths[index] || '100px',
        color: this.columnColors[index] || '#000',
        'flex-shrink': 0
      }
    },
    applyRowStyle(index) {
      return {
        color: this.rowColors[index] || '#55552'
      }
    },

//搜索statisticsData源数据
		filterData() {
			if (!this.searchText) {
				this.filteredData = this.userSaigeData;
				return;
			}
			this.filteredData = this.userSaigeData.filter(item =>
				item.designated_person.includes(this.searchText)
			);
		},
  }
};
</script>

<style>
.container {
  min-height: 10vh;
  padding: 0 20rpx;
  background-color: #f5f5f5;
}

.title {
  text-align: center;
  font-size: 36rpx;
  font-weight: bold;
  color: #ffffff;
  height: 50px;
  line-height: 50px;
  border-radius: 8px;
  margin-bottom: 10px;
  background: linear-gradient(to right, #004dc7, #328ebf);
  display: flex;
  justify-content: center;
  align-items: center;
}

.table-container {
  width: 100%;
  border: 1px solid #ddd;
  background: white;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  border-radius: 10rpx;


}

.table {
  width: 100%;
  min-width: 1200rpx;
  border-collapse: collapse;
}

th,
td {
  border: 1px solid #ddd;
  padding: 20rpx;
  text-align: center;
  white-space: nowrap;
  font-size: 14px;
  border-radius: 6px;
}

th {
  background-color: #4CAF50;
  color: white;
  position: sticky;
  top: 0;
  font-weight: normal;
}

tr:nth-child(even) {
  background-color: #f9f9f9;
}

tr:hover {
  background-color: #f1f1f1;
}
 .search-container {
  	margin-bottom: 10px;
  	background-color: #f8f8f8;
  }
  
  .search-input {
  	position: relative;
  	background-color: #fff;
  	border-radius: 40rpx;
  	padding: 10rpx 30rpx;
  	box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
  }
  .search-input input {
  	width: 100%;
  	height: 50rpx;
  	padding-right: 60rpx;

  }
  
  .search-icon {
  
  	position: absolute;
  	right: 30rpx;
  	top: 50%;
  	transform: translateY(-50%);
  	color: #999;
  }
/* 移动端适配 */
@media (max-width: 60px) {
  .table-container {
    max-height: 600vh;
  }

  th,
  td {
    padding: 15rpx 10rpx;
    font-size: 24rpx;
  }
}
</style>